using System;
using System.Data;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;

namespace PS.WebParts.Rollup.CalendarWebPart
{
    public class CalendarView : DataSourceView
    {
        private int    calendarType;
        private string startDateField;
        private string endDateField;
        private string titleField;
        private string allDayField;
        private string itemIdField;
        private string descField;
        private string locationField;
        private string recurrenceField;
        private string recurrenceExcField;
        private string linkField;
        private string urlField;
        private string direction;
        
        private CalendarDataSource source;

        public CalendarView(CalendarDataSource owner, string viewName)
            : base(owner, viewName)
        {
            source = owner;
            calendarType = owner.CalendarType;
            startDateField = owner.StartDateField;
            endDateField = owner.EndDateField;
            titleField = owner.TitleField;
            allDayField = owner.AllDayEventField;
            itemIdField = owner.ItemIdField;
            descField = owner.DescField;
            locationField = owner.LocationField;
            recurrenceField = owner.RecurrenceField;
            recurrenceExcField = owner.RecurrenceExcField;
            linkField = owner.WorkspaceLinkField;
            urlField = owner.DispFormUrlField;
            direction = owner.Direction;
        }

        protected override System.Collections.IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            SPCalendarItemCollection items = new SPCalendarItemCollection();

            ATabularDataSourceView view = (ATabularDataSourceView) source.DataSource.GetView(string.Empty);
            DataTable data = view.GetData();

            foreach (DataRow row in data.Rows)
            {
                DateTime startDate;
                SetDateTimeValue(out startDate, row, startDateField);
                if (DateTime.MinValue == startDate)
                {
                    //to do logged here
                    continue;
                }

                DateTime endDate;
                bool hasEndDate = SetDateTimeValue(out endDate, row, endDateField);

                string title;
                SetStringValue(out title, row, titleField);

                bool allDayEvent;
                SetBooleanValue(out allDayEvent, row, allDayField);

                string itemID;
                SetStringValue(out itemID, row, itemIdField);

                string description;
                SetStringValue(out description, row, descField);

                string location;
                SetStringValue(out location, row, locationField);

                bool recurrence;
                SetBooleanValue(out recurrence, row, recurrenceField);

                bool recurrenceExc;
                SetBooleanValue(out recurrenceExc, row, recurrenceExcField);

                string workspaceLink;
                SetStringValue(out workspaceLink, row, linkField);

                string dispFormUrl;
                SetStringValue(out dispFormUrl, row, urlField);

                try
                {
                    SPCalendarItem item = new SPCalendarItem(calendarType, startDate, endDate, hasEndDate, allDayEvent, title, itemID,
                        description, location, recurrence, recurrenceExc, workspaceLink, dispFormUrl, direction);

                    items.Add(item);
                }
                catch (NotSupportedException e)
                {
                    //to do logged here
                    continue;
                }
                catch (ArgumentException e)
                {
                    //to do logged here
                    continue;
                }
            }

            return items;
        }

        private static bool SetStringValue(out string value, DataRow row, string name)
        {
            if (!String.IsNullOrEmpty(name) && row.Table.Columns.Contains(name) && !row.IsNull(name))
            {
                value = row[name].ToString();
                return true;
            }
            else
            {
                value = null;
                return false;
            }
        }

        private static bool SetBooleanValue(out bool value, DataRow row, string name)
        {
            if (!String.IsNullOrEmpty(name) && row.Table.Columns.Contains(name) && !row.IsNull(name))
            {
                value = "1" == row[name].ToString();
                return true;
            }
            else
            {
                value = false;
                return false;
            }
        }
        
        private static bool SetDateTimeValue(out DateTime value, DataRow row, string name)
        {
            if (!String.IsNullOrEmpty(name) && row.Table.Columns.Contains(name) && !row.IsNull(name))
            {
                value = Convert.ToDateTime(row[name]);
                return true;
            }
            else
            {
                value = DateTime.MinValue;
                return false;
            }
        }
    }
}
